﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RN.ERP.DAL;
using RN.ERP.Entity.Authority;
using Fly.FlyCommon;
using RN.ERP.Entity;
using System.Data.Entity;
using RN.ERP.Entity.QD;
using Fly.Log;

namespace RN.ERP.BLL.QD
{
    public class AuthorityBLL : BaseAuthorityBLL
    {
        private QD_Context dbContext
        {
            get
            {
                return (QD_Context)base.baseContext;
            }
        }
        public AuthorityBLL()
        {
            base.baseContext = new QD_Context();
        }

        #region 角色管理 刘焕
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="title">名称</param>
        /// <param name="code">编号</param>
        /// <param name="areacode">地区编号</param>
        /// <param name="description">描述</param>
        /// <returns></returns>
        public override List<DTO.T_Sys_Roles> GetRoles(string title, string code, string areacode, string description)
        {
            var query = from a in dbContext.SysRoles
                        where a.IsDel == false
                        select a;

            if (!string.IsNullOrEmpty(title))
            {
                query = query.Where(r => r.Title.Contains(title));

            }
            if (!string.IsNullOrEmpty(code))
            {
                query = query.Where(r => r.Code.Contains(code));
            }
            if (!string.IsNullOrEmpty(areacode))
            {
                query = query.Where(r => r.AreaCode.Contains(areacode));
            }
            if (!string.IsNullOrEmpty(description))
            {
                query = query.Where(r => r.Description.Contains(description));
            }
            List<T_Sys_Roles> list = query.OrderByDescending(m => m.IsrtDate).ToList();
            List<DTO.T_Sys_Roles> lists = new List<DTO.T_Sys_Roles>();
            foreach (T_Sys_Roles item in list)
            {
                DTO.T_Sys_Roles role = new DTO.T_Sys_Roles();
                EntityHelper.ConvertEntity2OtherEntity(item, role);
                lists.Add(role);
            }
            return lists;
        }

        /// <summary>
        /// 根据角色GuID获取角色信息
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public override DTO.T_Sys_Roles GetRoleByGuId(Guid guid)
        {
            T_Sys_Roles role = dbContext.SysRoles.Where(m => m.RoleGuid == guid).FirstOrDefault();
            DTO.T_Sys_Roles roles = new DTO.T_Sys_Roles();
            EntityHelper.ConvertEntity2OtherEntity(role, roles);
            return roles;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public override DTO.OperationResult AddRole(DTO.T_Sys_Roles roleDTO)
        {
            T_Sys_Roles roles = new T_Sys_Roles();
            EntityHelper.ConvertEntity2OtherEntity(roleDTO, roles);
            DTO.OperationResult result = new DTO.OperationResult();
            try
            {
                dbContext.SysRoles.Add(roles);
                dbContext.SaveChanges();

                #region 添加操作日志
                DTO.UserPermission userLog = new DTO.UserPermission();
                string content = "添加角色信息！";
                string objectid = roles.RoleGuid.ToString();
                userLog.UserId = new Guid().ToString();
                userLog.UserName = "";
                userLog.FunctionId = DTO.FunctionId.角色模块;
                userLog.FunctionType = DTO.FunctionType.添加;
                InsertUserLog(content, objectid, userLog);
                #endregion

                result.Result = true;
                result.ReturnValue = null;
                result.Message = "角色添加成功！";
            }
            catch (Exception ex)
            {
                result.Message = "角色添加失败！" + ex.Message;
                result.Result = false;
            }
            return result;
        }

        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="roleDTO"></param>
        /// <returns></returns>
        public override DTO.OperationResult UpdateRole(DTO.T_Sys_Roles roles)
        {
            DTO.OperationResult result = new DTO.OperationResult();
            try
            {
                var role = dbContext.SysRoles.Where(r => r.RoleGuid == roles.RoleGuid).FirstOrDefault();
                if (role != null)
                {
                    role.Title = roles.Title;
                    role.Description = roles.Description;
                    role.Code = roles.Code;
                    role.AreaCode = roles.AreaCode;
                    role.GroupId = roles.GroupId;
                    dbContext.SaveChanges();

                    #region 添加操作日志
                    DTO.UserPermission userLog = new DTO.UserPermission();
                    string content = "修改角色信息！";
                    string objectid = role.RoleGuid.ToString();
                    userLog.UserId = new Guid().ToString();
                    userLog.UserName = "";
                    userLog.FunctionId = DTO.FunctionId.角色模块;
                    userLog.FunctionType = DTO.FunctionType.修改;
                    InsertUserLog(content, objectid, userLog);
                    #endregion

                    result.Result = true;
                    result.ReturnValue = null;
                    result.Message = "角色修改成功！";
                }
            }
            catch (Exception ex)
            {
                result.Message = "角色修改失败！" + ex.Message;
                result.Result = false;
            }
            return result;
        }

        /// <summary>
        /// 根据角色Guid获取人员信息
        /// </summary>
        /// <param name="roleGuid">角色Guid</param>
        /// <returns></returns>
        public override List<DTO.Sys_Users> GetUserListByRoleGuid(Guid roleGuid)
        {
            DbSet<T_Sys_Users> dbUser = dbContext.T_Sys_Users;
            DbSet<T_Sys_UserRole> dbUserRoles = dbContext.SysUserRoles;
            var query =
                from user in dbUser
                join role in dbUserRoles on user.UserGuid equals role.UserGuid
                where role.RoleGuid == roleGuid && role.IsDel == false
                select new
                {
                    user.UserGuid,
                    user.Name,
                    user.DeptName
                };
            var list = query.ToList();
            List<DTO.Sys_Users> lists = new List<DTO.Sys_Users>();
            foreach (var item in list)
            {
                DTO.Sys_Users user = new DTO.Sys_Users();
                EntityHelper.ConvertEntity2OtherEntity(item, user);
                lists.Add(user);
            }
            return lists;
        }

        /// <summary>
        /// 根据角色Guid和用户Guid删除用户的角色
        /// </summary>
        /// <param name="roleGuid">角色Guid</param>
        /// <param name="userGuid">用户Guid</param>
        /// <returns></returns>
        public override DTO.OperationResult DeleteByUserRole(string roleGuid, string userGuid)
        {
            DTO.OperationResult result = new DTO.OperationResult();
            try
            {
                Guid roleguid = Guid.Parse(roleGuid);
                Guid userguid = Guid.Parse(userGuid);
                var role = dbContext.SysUserRoles.Where(o => o.RoleGuid == roleguid && o.UserGuid == userguid).FirstOrDefault();
                role.IsDel = true;
                dbContext.SaveChanges();

                #region 添加操作日志
                DTO.UserPermission userLog = new DTO.UserPermission();
                string content = "删除用户角色信息！(逻辑删除：设置IsDel字段为：true)";
                string objectid = role.ID.ToString();
                userLog.UserId = new Guid().ToString();
                userLog.UserName = "";
                userLog.FunctionId = DTO.FunctionId.人员关联角色;
                userLog.FunctionType = DTO.FunctionType.删除;
                InsertUserLog(content, objectid, userLog);
                #endregion

                result.Result = true;
                result.ReturnValue = null;
                result.Message = "删除成功！";
            }
            catch (Exception ex)
            {
                result.Message = "删除失败！" + ex.Message;
                result.Result = false;
            }
            return result;
        }

        /// <summary>
        /// 判断人员是否有角色
        /// </summary>
        /// <param name="roleGuid"></param>
        /// <param name="userGuid"></param>
        /// <returns></returns>
        public override DTO.OperationResult GetUserRoleByUserRole(string roleGuid, string userGuid, string userID)
        {
            DTO.OperationResult result = new DTO.OperationResult();
            try
            {
                Guid roleguid = Guid.Parse(roleGuid);
                Guid userguid = Guid.Parse(userGuid);
                var role = dbContext.SysUserRoles.Where(o => o.RoleGuid == roleguid && o.UserGuid == userguid).FirstOrDefault();
                if (role != null)
                {
                    if ((bool)role.IsDel && userID != null)
                    {
                        role.IsDel = false;
                        role.IsrtDate = DateTime.Now;
                        role.IsrtUseID = Guid.Parse(userID);
                        dbContext.SaveChanges();

                        #region 添加操作日志
                        DTO.UserPermission userLog = new DTO.UserPermission();
                        string content = "根据用户GUID和角色GUID获取记录！如已删除此记录，将重新添加此记录！(添加：设置IsDel字段为：false)";
                        string objectid = role.ID.ToString();
                        userLog.UserId = new Guid().ToString();
                        userLog.UserName = "";
                        userLog.FunctionId = DTO.FunctionId.人员关联角色;
                        userLog.FunctionType = DTO.FunctionType.添加;
                        InsertUserLog(content, objectid, userLog);
                        #endregion
                    }
                    result.Result = true;
                    result.ReturnValue = 1;
                    result.Message = "操作成功！";
                }
                else
                {
                    result.Result = true;
                    result.ReturnValue = 0;
                    result.Message = "操作成功！";
                }
            }
            catch (Exception ex)
            {
                result.Message = "操作失败！" + ex.Message;
                result.Result = false;
            }
            return result;
        }

        /// <summary>
        /// 根绝角色Guid获取人员角色表
        /// </summary>
        /// <param name="roleGuid"></param>
        /// <returns></returns>
        public override List<DTO.T_Sys_UserRole> GetUserRolesByRoleGuid(Guid roleGuid)
        {
            DbSet<T_Sys_UserRole> dbUserRoles = dbContext.SysUserRoles;
            var query =
                from userroles in dbUserRoles
                where userroles.RoleGuid == roleGuid && userroles.IsDel == false
                orderby userroles.IsrtDate descending
                select userroles;
            List<T_Sys_UserRole> list = query.OrderByDescending(m => m.IsrtDate).ToList();
            List<DTO.T_Sys_UserRole> lists = new List<DTO.T_Sys_UserRole>();
            foreach (T_Sys_UserRole item in list)
            {
                DTO.T_Sys_UserRole user = new DTO.T_Sys_UserRole();
                EntityHelper.ConvertEntity2OtherEntity(item, user);
                lists.Add(user);
            }
            return lists;
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleGuid"></param>
        /// <returns></returns>
        public override DTO.OperationResult DeleteRole(string roleGuid)
        {
            DTO.OperationResult result = new DTO.OperationResult();
            try
            {
                Guid roleguid = Guid.Parse(roleGuid);
                var role = dbContext.SysRoles.Where(o => o.RoleGuid == roleguid).FirstOrDefault();
                role.IsDel = true;
                dbContext.SaveChanges();

                #region 添加操作日志
                DTO.UserPermission userLog = new DTO.UserPermission();
                string content = "删除角色信息！(逻辑删除：设置IsDel字段为：true)";
                string objectid = role.RoleGuid.ToString();
                userLog.UserId = new Guid().ToString();
                userLog.UserName = "";
                userLog.FunctionId = DTO.FunctionId.角色模块;
                userLog.FunctionType = DTO.FunctionType.删除;
                InsertUserLog(content, objectid, userLog);
                #endregion

                result.Result = true;
                result.ReturnValue = null;
                result.Message = "删除成功！";
            }
            catch (Exception ex)
            {
                result.Message = "删除失败！" + ex.Message;
                result.Result = false;
            }
            return result;
        }
        #endregion 角色管理

        #region 分配功能权限 刘焕
        /// <summary>
        /// 给用户或角色分配功能
        /// </summary>
        /// <param name="funcDTO"></param>
        /// <returns></returns>
        public override DTO.OperationResult AddFuncPermission(DTO.T_Sys_FuncPermission funcDTO)
        {
            T_Sys_FuncPermission func = new T_Sys_FuncPermission();
            EntityHelper.ConvertEntity2OtherEntity(funcDTO, func);
            DTO.OperationResult result = new DTO.OperationResult();
            try
            {
                dbContext.SysFuncPermission.Add(func);
                dbContext.SaveChanges();

                #region 添加操作日志
                DTO.UserPermission userLog = new DTO.UserPermission();
                string content = "给角色添加功能！";
                string objectid = func.ID.ToString();
                userLog.UserId = new Guid().ToString();
                userLog.UserName = "";
                userLog.FunctionId = DTO.FunctionId.角色关联功能;
                userLog.FunctionType = DTO.FunctionType.添加;
                InsertUserLog(content, objectid, userLog);
                #endregion

                result.Result = true;
                result.ReturnValue = null;
                result.Message = "添加成功！";
            }
            catch (Exception ex)
            {
                result.Message = "添加失败！" + ex.Message;
                result.Result = false;
            }
            return result;
        }

        /// <summary>
        /// 根据功能Guid和人员或角色Guid删除关联
        /// </summary>
        /// <param name="funcGuid">功能Guid</param>
        /// <param name="objGuid">人员或角色Guid</param>
        /// <returns></returns>
        public override DTO.OperationResult DelFuncPermission(Guid funcGuid, Guid objGuid)
        {
            DTO.OperationResult result = new DTO.OperationResult();
            try
            {
                var func = dbContext.SysFuncPermission.Where(f => f.FunctionGuid == funcGuid && f.ObjectGuid == objGuid).FirstOrDefault();
                func.IsDel = true;
                dbContext.SaveChanges();

                #region 添加操作日志
                DTO.UserPermission userLog = new DTO.UserPermission();
                string content = "根据功能Guid和功能Guid获取记录是否存在！如果存在就删除此功能！(删除：设置IsDel字段为：true)";
                string objectid = func.ID.ToString();
                userLog.UserId = new Guid().ToString();
                userLog.UserName = "";
                userLog.FunctionId = DTO.FunctionId.角色关联功能;
                userLog.FunctionType = DTO.FunctionType.删除;
                InsertUserLog(content, objectid, userLog);
                #endregion

                result.Result = true;
                result.ReturnValue = null;
                result.Message = "删除成功！";
            }
            catch (Exception ex)
            {
                result.Message = "删除失败！" + ex.Message;
                result.Result = false;
            }
            return result;
        }

        /// <summary>
        /// 根据角色(人员)Guid获取权限
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="type">角色(人员)Guid</param>
        /// <returns></returns>
        public override List<DTO.T_Sys_FuncPermission> GetFuncPermission(Guid guid, string appid)
        {
            List<T_Sys_FuncPermission> list;
            list = dbContext.SysFuncPermission.Where(f => f.ObjectGuid == guid && f.IsDel == false).ToList();
            List<DTO.T_Sys_FuncPermission> lists = new List<DTO.T_Sys_FuncPermission>();
            foreach (T_Sys_FuncPermission item in list)
            {
                DTO.T_Sys_FuncPermission role = new DTO.T_Sys_FuncPermission();
                EntityHelper.ConvertEntity2OtherEntity(item, role);
                lists.Add(role);
            }
            return lists;
        }

        /// <summary>
        /// 根据功能Guid和角色（人员）Guid判断是否添加该功能
        /// </summary>
        /// <param name="objGuid"></param>
        /// <param name="funcGuid"></param>
        /// <param name="userID"></param>
        /// <returns></returns>
        public override DTO.OperationResult GetFuncByFuncObj(string objGuid, string funcGuid, string userID)
        {
            DTO.OperationResult result = new DTO.OperationResult();
            try
            {
                Guid objguid = Guid.Parse(objGuid);
                Guid funcguid = Guid.Parse(funcGuid);
                var func = dbContext.SysFuncPermission.Where(o => o.ObjectGuid == objguid && o.FunctionGuid == funcguid).FirstOrDefault();
                if (func != null)
                {
                    if ((bool)func.IsDel && userID != null)
                    {
                        func.IsDel = false;
                        func.IsrtDate = DateTime.Now;
                        func.IsrtUseID = Guid.Parse(userID);
                        dbContext.SaveChanges();

                        #region 添加操作日志
                        DTO.UserPermission userLog = new DTO.UserPermission();
                        string content = "根据角色Guid判断是否添加该功能!如果存在已删除记录将重新添加此记录！(添加：设置IsDel字段为：false)";
                        string objectid = func.ID.ToString();
                        userLog.UserId = new Guid().ToString();
                        userLog.UserName = "";
                        userLog.FunctionId = DTO.FunctionId.角色关联功能;
                        userLog.FunctionType = DTO.FunctionType.添加;
                        InsertUserLog(content, objectid, userLog);
                        #endregion
                    }
                    result.Result = true;
                    result.ReturnValue = 1;
                    result.Message = "操作成功！";
                }
                else
                {
                    result.Result = true;
                    result.ReturnValue = 0;
                    result.Message = "操作成功！";
                }
            }
            catch (Exception ex)
            {
                result.Message = "操作失败！" + ex.Message;
                result.Result = false;
            }
            return result;
        }

        /// <summary>
        /// 根据用户Guid和功能Guid 检测该用户是否存在此功能 --孙正滨
        /// </summary>
        /// <param name="userGuid">用户Guid</param>
        /// <param name="funcGuid">功能Guid</param>
        /// <returns></returns>
        public override DTO.OperationResult CheckFuncByUserGuid(string userGuid, string funcGuid)
        {
            DTO.OperationResult result = new DTO.OperationResult();
            try
            {
                Guid objguid = Guid.Parse(userGuid);
                Guid funcguid = Guid.Parse(funcGuid);
                var func = dbContext.SysFuncPermission.Where(o => o.ObjectGuid == objguid && o.FunctionGuid == funcguid).FirstOrDefault();
                if (func != null)
                {
                    //func.OperType = 1;
                    dbContext.SaveChanges();

                    #region 添加操作日志
                    DTO.UserPermission userLog = new DTO.UserPermission();
                    string content = "根据用户Guid和功能Guid 检测该用户是否存在此功能!如果存在就删除此记录！(逻辑删除：设置OperType字段为：1)";
                    string objectid = func.ID.ToString();
                    userLog.UserId = new Guid().ToString();
                    userLog.UserName = "";
                    userLog.FunctionId = DTO.FunctionId.人员关联功能;
                    userLog.FunctionType = DTO.FunctionType.删除;
                    InsertUserLog(content, objectid, userLog);
                    #endregion

                    result.Result = true;
                    result.ReturnValue = 1;
                    result.Message = "操作成功！";
                }
                else
                {
                    result.Result = true;
                    result.ReturnValue = 0;
                    result.Message = "操作成功！";
                }
            }
            catch (Exception ex)
            {
                result.Message = "操作失败！" + ex.Message;
                result.Result = false;
            }
            return result;
        }

        /// <summary>
        /// 根据角色(人员)Guid获取权限 --孙正滨
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="type">人员Guid</param>
        /// <returns></returns>
        public override List<DTO.T_Sys_FuncPermission> GetFuncListByUserGuid(Guid guid)
        {
            //List<T_Sys_FuncPermission> list;
            //list = dbContext.SysFuncPermission.Where(f => f.ObjectGuid == guid && f.OperType == 0).ToList();
            //List<DTO.T_Sys_FuncPermission> lists = new List<DTO.T_Sys_FuncPermission>();
            //foreach (T_Sys_FuncPermission item in list)
            //{
            //    DTO.T_Sys_FuncPermission role = new DTO.T_Sys_FuncPermission();
            //    EntityHelper.ConvertEntity2OtherEntity(item, role);
            //    lists.Add(role);
            //}
            //return lists;
            return null;
        }

        /// <summary>
        /// 根据人员Guid判断是否添加该功能 -- 孙正滨
        /// </summary>
        /// <param name="objGuid">用户GUID</param>
        /// <param name="funcGuid">功能GUID</param>
        /// <param name="userID">添加人GUID</param>
        /// <returns></returns>
        public override DTO.OperationResult ChechFuncByFuncObj(string objGuid, string funcGuid, string userID)
        {
            //DTO.OperationResult result = new DTO.OperationResult();
            //try
            //{
            //    Guid objguid = Guid.Parse(objGuid);
            //    Guid funcguid = Guid.Parse(funcGuid);
            //    var func = dbContext.SysFuncPermission.Where(o => o.ObjectGuid == objguid && o.FunctionGuid == funcguid).FirstOrDefault();
            //    if (func != null)
            //    {
            //        if (func.OperType == 1 && userID != null)
            //        {
            //            //func.OperType = 0;
            //            func.IsrtDate = DateTime.Now;
            //            func.IsrtUseID = Guid.Parse(userID);
            //            dbContext.SaveChanges();

            //            #region 添加操作日志
            //            DTO.UserPermission userLog = new DTO.UserPermission();
            //            string content = " 根据人员Guid判断是否添加该功能!如果存在已删除记录将重新添加此记录！(添加：设置OperType字段为：0)";
            //            string objectid = func.ID.ToString();
            //            userLog.UserId = new Guid().ToString();
            //            userLog.UserName = "";
            //            userLog.FunctionId = DTO.FunctionId.人员关联功能;
            //            userLog.FunctionType = DTO.FunctionType.添加;
            //            InsertUserLog(content, objectid, userLog);
            //            #endregion
            //        }
            //        result.Result = true;
            //        result.ReturnValue = 1;
            //        result.Message = "操作成功！";
            //    }
            //    else
            //    {
            //        result.Result = true;
            //        result.ReturnValue = 0;
            //        result.Message = "操作成功！";
            //    }
            //}
            //catch (Exception ex)
            //{
            //    result.Message = "操作失败！" + ex.Message;
            //    result.Result = false;
            //}
            //return result;
            return null;
        }
        #endregion

        #region 添加日志
        /// <summary>
        /// 插入记录
        /// </summary>
        /// <param name="content"></param>
        /// <param name="objectid"></param>
        /// <param name="user"></param>
        private void InsertUserLog(string content, string objectid, DTO.UserPermission user)
        {
            UserLogManager.Log(new User_Log()
            {
                Content = content, //操作说明
                ObjectId = objectid, //记录主键
                IsrtUser = user.UserId, //操作人人ID
                IsrtUserName = user.UserName, //操作人姓名
                IsrtUserArea = "02", //操作人地区号
                FunctionId = ((int)user.FunctionId).ToString(), //操作功能编号
                FunctionType = ((int)user.FunctionType).ToString(), //操作功能编号
                AppId = "99" //项目编号
            }); ;
        }
        #endregion

        #region UC控件调用
        public override List<DTO.tb_Emp_TitleType> GetPsnQualLevelByType(int nid)
        {
            List<int> titleclass = (from a in dbContext.tb_Emp_TitleClass
                                    where a.ClassID == nid && a.Token == "JiBie"
                                    select a.NID).ToList();

            var level = (from b in dbContext.tb_Emp_TitleType
                         where titleclass.Contains(b.NID)
                         select b).ToList();
            //if (level == null||level.Count==0)
            //{
            //    level = dbContext.tb_Emp_TitleClass.Where(m => m.ClassID == -1 && m.Token == "JiBie").ToList();
            //}

            List<DTO.tb_Emp_TitleType> ret = new List<DTO.tb_Emp_TitleType>();
            foreach (tb_Emp_TitleType item in level)
            {
                DTO.tb_Emp_TitleType lv = new DTO.tb_Emp_TitleType();
                Fly.FlyCommon.EntityHelper.ConvertEntity2OtherEntity(item, lv);
                ret.Add(lv);
            }
            return ret;
        }
        #endregion UC控件调用

        #region 功能管理 蓝立明
        /// <summary>
        /// 功能更新
        /// </summary>
        /// <returns></returns>
        public override DTO.OperationResult UpdateFunction(DTO.T_Sys_Function function)
        {
            DbSet<T_Sys_Function> dbFunction = dbContext.SysFunction;
            DTO.OperationResult result = new DTO.OperationResult();
            DTO.T_Sys_Function func = new DTO.T_Sys_Function();
            result.Message = "更新失败";
            result.Result = false;
            result.ReturnValue = 0;
            try
            {
                var ret = dbFunction.Where(m => m.FunctionGuid == function.FunctionGuid && m.IsDel == false).FirstOrDefault();
                if (ret == null)
                {
                    result.Message = "找不到功能记录（GUID=" + function.FunctionGuid + ")";
                }
                Fly.FlyCommon.EntityHelper.ConvertEntity2OtherEntity(function, func);
                ret.Code = func.Code;
                ret.Title = func.Title;
                ret.DESCRIPTION = func.DESCRIPTION;
                ret.AppID = func.AppID;
                ret.Url = func.Url;
                ret.IsMenu = func.IsMenu;
                ret.ParenGuid = func.ParenGuid;
                ret.RootGuid = func.RootGuid;
                dbContext.SaveChanges();

                #region 添加操作日志
                DTO.UserPermission userLog = new DTO.UserPermission();
                string content = "修改功能！";
                string objectid = ret.FunctionGuid.ToString();
                userLog.UserId = new Guid().ToString();
                userLog.UserName = "";
                userLog.FunctionId = DTO.FunctionId.功能模块;
                userLog.FunctionType = DTO.FunctionType.修改;
                InsertUserLog(content, objectid, userLog);
                #endregion

                result.Message = "更新成功";
                result.ReturnValue = 1;
                result.Result = true;
            }
            catch (Exception ex)
            {
                result.Message = "更新失败" + ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 获取功能列表
        /// </summary>
        /// <param name="code">编号</param>
        /// <param name="title">名称</param>
        /// <param name="appID">项目ID</param>
        /// <param name="parentGuid">父节点</param>
        /// <param name="rootGuid">根节点</param>
        /// <param name="isMenu">是否菜单</param>
        /// <param name="isDel">是否删除</param>
        /// <returns></returns>
        public override IList<DTO.T_Sys_Function> GetFunctionList(DTO.FuncQueryCondition condition)
        {
            DbSet<T_Sys_Function> dbFunction = dbContext.SysFunction;
            var query =
               from Function in dbFunction
               select Function;
            if (!String.IsNullOrEmpty(condition.Code))
            {
                query = query.Where(o => o.Code.Contains(condition.Code));
            }
            if (!String.IsNullOrEmpty(condition.Title))
            {
                query = query.Where(o => o.Title.Contains(condition.Title));
            }
            if (!String.IsNullOrEmpty(condition.AppID))
            {
                query = query.Where(o => o.AppID == condition.AppID);
            }
            if (condition.ParentGuid != null)
            {
                query = query.Where(o => o.ParenGuid == condition.ParentGuid);
            }
            if (condition.RootGuid != null)
            {
                query = query.Where(o => o.RootGuid == condition.RootGuid);
            }
            if (condition.IsMenu != null)
            {
                query = query.Where(o => o.IsMenu == condition.IsMenu);
            }
            if (condition.IsDel != null)
            {
                query = query.Where(o => o.IsDel == condition.IsDel);
            }
            IList<T_Sys_Function> funclist = query.ToList();
            IList<DTO.T_Sys_Function> ret = new List<DTO.T_Sys_Function>();
            foreach (T_Sys_Function item in funclist)
            {
                DTO.T_Sys_Function func = new DTO.T_Sys_Function();
                Fly.FlyCommon.EntityHelper.ConvertEntity2OtherEntity(item, func);
                ret.Add(func);
            }

            return ret.ToList();
        }

        /// <summary>
        /// 角色删除
        /// </summary>
        /// <returns></returns>
        public override DTO.OperationResult DeleteFunction(Guid funcGuid)
        {
            DbSet<T_Sys_Function> dbFunction = dbContext.SysFunction;
            DTO.OperationResult result = new DTO.OperationResult();
            result.Message = "删除失败";
            result.Result = false;
            result.ReturnValue = 0;
            try
            {
                var role = dbFunction.Where(o => o.FunctionGuid == funcGuid).FirstOrDefault();
                role.IsDel = true;
                dbContext.SaveChanges();

                #region 添加操作日志
                DTO.UserPermission userLog = new DTO.UserPermission();
                string content = "删除功能！(删除：设置IsDel字段为：true)";
                string objectid = role.FunctionGuid.ToString();
                userLog.UserId = new Guid().ToString();
                userLog.UserName = "";
                userLog.FunctionId = DTO.FunctionId.功能模块;
                userLog.FunctionType = DTO.FunctionType.删除;
                InsertUserLog(content, objectid, userLog);
                #endregion

                result.Message = "删除成功";
                result.Result = true;
                result.ReturnValue = 1;
            }
            catch (Exception ex)
            {
                result.Message = "删除失败" + ex.Message;
            }
            return result;
        }

        public override DTO.OperationResult AddFunction(DTO.T_Sys_Function function)
        {
            //DbSet<T_Sys_Function> dbFunction = dbContext.SysFunction;
            DTO.OperationResult result = new DTO.OperationResult();
            result.Message = "添加失败";
            result.Result = false;
            result.ReturnValue = 0;
            try
            {
                T_Sys_Function func = new T_Sys_Function();
                Fly.FlyCommon.EntityHelper.ConvertEntity2OtherEntity(function, func);
                dbContext.SysFunction.Add(func);

                #region 添加操作日志
                DTO.UserPermission userLog = new DTO.UserPermission();
                string content = "添加功能！";
                string objectid = func.FunctionGuid.ToString();
                userLog.UserId = new Guid().ToString();
                userLog.UserName = "";
                userLog.FunctionId = DTO.FunctionId.功能模块;
                userLog.FunctionType = DTO.FunctionType.添加;
                InsertUserLog(content, objectid, userLog);
                #endregion

                dbContext.SaveChanges();
                result.Message = "添加成功";
                result.Result = true;
                result.ReturnValue = 1;
            }
            catch (Exception ex)
            {
                result.Message = "添加失败" + ex.Message;
            }
            return result;
        }

        public override DTO.T_Sys_Function GetFunctionByGuid(Guid funcGuid)
        {
            DbSet<T_Sys_Function> dbFunction = dbContext.SysFunction;
            DTO.T_Sys_Function result = new DTO.T_Sys_Function();
            var function = dbFunction.Where(o => o.FunctionGuid == funcGuid && o.IsDel == false).FirstOrDefault();
            if (function != null)
            {
                Fly.FlyCommon.EntityHelper.ConvertEntity2OtherEntity(function, result);
            }
            return result;
        }

        public override IList<DTO.T_Sys_Function> GetMenuList(Guid userGuid, string appID)
        {
            //DbSet<T_Sys_Function> dbFunction = dbContext.SysFunction;
            //DbSet<T_Sys_UserRole> dbUserRoles = dbContext.SysUserRoles;
            //DbSet<T_Sys_Roles> dbRoles = dbContext.SysRoles;
            //DbSet<T_Sys_FuncPermission> dbFuncRoles = dbContext.SysFuncPermission;
            //DbSet<T_Sys_Users> dbUser = dbContext.T_Sys_Users;
            ////获取用户下的角色,角色下的功能
            //var query = from user in dbContext.T_Sys_Users
            //            join userRoles in dbContext.SysUserRoles on user.UserGuid equals userRoles.UserGuid into userUserRoles
            //            from userUserRole in userUserRoles.DefaultIfEmpty()
            //            join role in dbContext.SysRoles on userUserRole.RoleGuid equals role.RoleGuid
            //            join funcRoles in dbContext.SysFuncPermission on userUserRole.RoleGuid equals funcRoles.ObjectGuid into userFuncRoles
            //            from userFuncRole in userFuncRoles.DefaultIfEmpty()
            //            from function in dbContext.SysFunction
            //            where user.UserGuid == userGuid && userFuncRole.Type == 0 && function.FunctionGuid == userFuncRole.FunctionGuid && function.IsDel == false && function.IsMenu == true && role.IsDel == false
            //            select new DTO.T_Sys_Function { FunctionGuid = function.FunctionGuid, Title = function.Title, DESCRIPTION = function.DESCRIPTION, Url = function.Url, RootGuid = function.RootGuid, ParenGuid = function.ParenGuid, AppID = function.AppID };

            ////获取用户的特有功能
            //int operType = 0;
            //var queryOper = from user in dbContext.T_Sys_Users
            //                join funcRoles in dbContext.SysFuncPermission on user.UserGuid equals funcRoles.ObjectGuid into userFuncRoles
            //                from userFuncRole in userFuncRoles.DefaultIfEmpty()
            //                from function in dbContext.SysFunction
            //                where userFuncRole.Type == 1 && function.FunctionGuid == userFuncRole.FunctionGuid && function.IsDel == false && function.IsMenu == true
            //                && userFuncRole.OperType == operType
            //                select new DTO.T_Sys_Function { FunctionGuid = function.FunctionGuid, Title = function.Title, DESCRIPTION = function.DESCRIPTION, Url = function.Url, RootGuid = function.RootGuid, ParenGuid = function.ParenGuid, AppID = function.AppID };
            //if (!String.IsNullOrEmpty(appID))
            //{
            //    query = query.Where(o => o.AppID == appID);
            //    queryOper = query.Where(o => o.AppID == appID);
            //}
            //IList<DTO.T_Sys_Function> funcRole = query.ToList();
            ////添加个体功能
            //operType = 0;
            //IList<DTO.T_Sys_Function> funcAdd = queryOper.ToList();
            //if (funcAdd.Count > 0)
            //{
            //    foreach (var item in funcAdd)
            //    {
            //        int cnt = -1;
            //        cnt = funcRole.Where(o => o.FunctionGuid == item.FunctionGuid).ToList().Count();
            //        if (cnt == 0)
            //        {
            //            funcRole.Add(item);
            //        }
            //    }
            //}
            ////删除个体功能
            //operType = 1;
            //IList<DTO.T_Sys_Function> funcMinus = queryOper.ToList();
            //if (funcMinus.Count > 0)
            //{
            //    foreach (var item in funcMinus)
            //    {
            //        int cnt = -1;
            //        cnt = funcRole.Where(o => o.FunctionGuid == item.FunctionGuid).ToList().Count();
            //        if (cnt > 0)
            //        {
            //            funcRole.Remove(item);
            //        }
            //    }
            //}
            //return funcRole.ToList();
            return null;
        }

        /// <summary>
        /// 功能添加控件
        /// </summary>
        /// <param name="conFuncDTO"></param>
        /// <returns></returns>
        public override DTO.OperationResult AddConFunction(DTO.T_Sys_ControlFunction conFuncDTO)
        {
            DbSet<T_Sys_ControlFunction> dbConFunction = dbContext.SysConFunction;
            T_Sys_ControlFunction conFunc = new T_Sys_ControlFunction();
            EntityHelper.ConvertEntity2OtherEntity(conFuncDTO, conFunc);
            DTO.OperationResult result = new DTO.OperationResult();
            result.Message = "添加失败";
            result.Result = false;
            result.ReturnValue = 0;
            try
            {
                var rec = dbConFunction.Where(o => o.CtrlGuid == conFunc.CtrlGuid && o.FunctionGuid == conFunc.FunctionGuid).FirstOrDefault();
                if (rec != null)
                {
                    result.Message = "该数据已存在";
                    result.Result = false;
                    result.ReturnValue = 2;
                }
                else
                {
                    dbConFunction.Add(conFunc);
                    dbContext.SaveChanges();

                    #region 添加操作日志
                    DTO.UserPermission userLog = new DTO.UserPermission();
                    string content = "为功能分配控件！";
                    string objectid = conFunc.ID.ToString();
                    userLog.UserId = new Guid().ToString();
                    userLog.UserName = "";
                    userLog.FunctionId = DTO.FunctionId.功能关联控件;
                    userLog.FunctionType = DTO.FunctionType.添加;
                    InsertUserLog(content, objectid, userLog);
                    #endregion

                    result.Result = true;
                    result.ReturnValue = 1;
                    result.Message = "添加成功！";
                }
            }
            catch (Exception ex)
            {
                result.Message = "添加失败！" + ex.Message;
            }
            return result;
        }
        // 获取功能控件列表
        public override IList<DTO.T_Sys_ControlFunction> GetConFunctionList(Guid funcGuid)
        {
            DbSet<T_Sys_ControlFunction> dbConFunction = dbContext.SysConFunction;
            DbSet<T_Sys_Function> dbFunction = dbContext.SysFunction;
            var query = from function in dbFunction
                        join conFunction in dbConFunction on function.FunctionGuid equals conFunction.FunctionGuid into funcConFuncs
                        from funcConFunc in funcConFuncs.DefaultIfEmpty()
                        where function.IsDel == false && funcConFunc.FunctionGuid == funcGuid
                        select funcConFunc;
            IList<T_Sys_ControlFunction> conFuncList = query.ToList();
            IList<DTO.T_Sys_ControlFunction> conFuncDTOList = new List<DTO.T_Sys_ControlFunction>();
            foreach (T_Sys_ControlFunction item in conFuncList)
            {
                DTO.T_Sys_ControlFunction conFuncDTO = new DTO.T_Sys_ControlFunction();
                EntityHelper.ConvertEntity2OtherEntity(item, conFuncDTO);
                conFuncDTOList.Add(conFuncDTO);
            }
            return conFuncDTOList;
        }

        //操作功能控件列表
        public override DTO.OperationResult OperateConFunction(Guid funcGuid, Guid conGuid, DTO.OperationType type)
        {
            DbSet<T_Sys_ControlFunction> dbConFunction = dbContext.SysConFunction;
            DTO.OperationResult result = new DTO.OperationResult();
            result.Message = "操作失败";
            result.Result = false;
            result.ReturnValue = 0;
            try
            {
                if (type == DTO.OperationType.Add)
                {
                    var conFunc = dbConFunction.Where(o => o.FunctionGuid == funcGuid && o.CtrlGuid == conGuid).FirstOrDefault();
                    if (conFunc != null)
                    {
                        conFunc.IsDel = false;
                        dbContext.SaveChanges();

                        #region 添加操作日志
                        DTO.UserPermission userLog = new DTO.UserPermission();
                        string content = "根据功能GUID和控件GUID获取记录是否已被删除！如记录已删除将重新添加此记录！(添加：设置IsDel字段为：flase)";
                        string objectid = conFunc.ID.ToString();
                        userLog.UserId = new Guid().ToString();
                        userLog.UserName = "";
                        userLog.FunctionId = DTO.FunctionId.功能关联控件;
                        userLog.FunctionType = DTO.FunctionType.添加;
                        InsertUserLog(content, objectid, userLog);
                        #endregion

                        result.Message = "记录恢复,添加成功";
                        result.Result = true;
                        result.ReturnValue = 1;
                    }
                }
                if (type == DTO.OperationType.Delete)
                {
                    var conFunc = dbConFunction.Where(o => o.FunctionGuid == funcGuid && o.CtrlGuid == conGuid).FirstOrDefault();
                    conFunc.IsDel = true;
                    dbContext.SaveChanges();

                    #region 添加操作日志
                    DTO.UserPermission userLog = new DTO.UserPermission();
                    string content = "根据功能GUID和控件GUID获取记录是否被删除！如没有被删除将删除此记录！(添加：设置IsDel字段为：true)";
                    string objectid = conFunc.ID.ToString();
                    userLog.UserId = new Guid().ToString();
                    userLog.UserName = "";
                    userLog.FunctionId = DTO.FunctionId.功能关联控件;
                    userLog.FunctionType = DTO.FunctionType.删除;
                    InsertUserLog(content, objectid, userLog);
                    #endregion

                    result.Message = "删除成功";
                    result.Result = true;
                    result.ReturnValue = 2;
                }
            }
            catch (Exception ex)
            {
                result.Message = "操作失败！" + ex.Message;
            }
            return result;
        }
        #endregion

        #region 用户角色管理 作者：孙正滨
        //获取企业列表(绑定到下拉菜单)
        //public override List<DTO.T_Sys_Users> GetUserList(string corpName, int pageSize, int pageNum)
        //{
        //    var query = from a in dbContext.T_Sys_Users
        //                select a;

        //    if (!string.IsNullOrEmpty(corpName))
        //    {
        //        query = query.Where(r => r.DeptName.Contains(corpName));

        //    }
        //    List<T_Sys_Users> list = query.OrderBy(m => m.ID).Skip(pageNum * pageSize).Take(pageSize).ToList();
        //    List<DTO.T_Sys_Users> lists = new List<DTO.T_Sys_Users>();
        //    foreach (T_Sys_Users item in list)
        //    {
        //        DTO.T_Sys_Users role = new DTO.T_Sys_Users();
        //        EntityHelper.ConvertEntity2OtherEntity(item, role);
        //        lists.Add(role);
        //    }
        //    return lists;
        //}

        // 获取角色列表
        public override List<DTO.T_Sys_UserRole> GetUserRoles(Guid userGuid)
        {
            var query =
                from userroles in dbContext.SysUserRoles
                where userroles.UserGuid == userGuid && userroles.IsDel == false
                orderby userroles.IsrtDate descending
                select userroles;
            List<T_Sys_UserRole> list = query.ToList();
            List<DTO.T_Sys_UserRole> lists = new List<DTO.T_Sys_UserRole>();
            foreach (T_Sys_UserRole item in list)
            {
                DTO.T_Sys_UserRole role = new DTO.T_Sys_UserRole();
                EntityHelper.ConvertEntity2OtherEntity(item, role);
                lists.Add(role);
            }
            return lists;
        }

        // 获取用户角色列表
        public override List<DTO.T_Sys_UserRole> GetUserRolesList(Guid userGuid, Guid roleGuid)
        {
            var query =
                from userroles in dbContext.SysUserRoles
                where userroles.UserGuid == userGuid && userroles.RoleGuid == roleGuid
                orderby userroles.IsrtDate descending
                select userroles;
            List<T_Sys_UserRole> list = query.ToList();
            List<DTO.T_Sys_UserRole> lists = new List<DTO.T_Sys_UserRole>();
            foreach (T_Sys_UserRole item in list)
            {
                DTO.T_Sys_UserRole role = new DTO.T_Sys_UserRole();
                EntityHelper.ConvertEntity2OtherEntity(item, role);
                lists.Add(role);
            }
            return lists;
        }

        /// <summary>
        /// 根据用户角色ID获取用户角色信息
        /// </summary>
        /// <param name="id">用户角色ID</param>
        /// <returns></returns>
        public override DTO.T_Sys_UserRole GetUserRoleById(int id)
        {
            T_Sys_UserRole role = dbContext.SysUserRoles.Where(m => m.ID == id).FirstOrDefault();
            DTO.T_Sys_UserRole roles = new DTO.T_Sys_UserRole();
            EntityHelper.ConvertEntity2OtherEntity(role, roles);
            return roles;
        }

        /// <summary>
        /// 根据用户角色Guid获取用户角色信息
        /// </summary>
        /// <param name="guid">用户角色Guid</param>
        /// <returns></returns>
        public override DTO.T_Sys_UserRole GetUserRoleByGuId(Guid roleGuid)
        {
            T_Sys_UserRole role = dbContext.SysUserRoles.Where(m => m.RoleGuid == roleGuid).FirstOrDefault();
            DTO.T_Sys_UserRole roles = new DTO.T_Sys_UserRole();
            EntityHelper.ConvertEntity2OtherEntity(role, roles);
            return roles;
        }

        /// <summary>
        /// 给用户分配角色
        /// </summary>
        /// <param name="userGuid">用户GUID</param>
        /// <param name="roleGuid">角色GUID</param>
        /// <param name="userID">添加人GUID</param>
        /// <returns></returns>
        public override DTO.OperationResult AddUserRole(DTO.T_Sys_UserRole userRoleDTO)
        {
            T_Sys_UserRole userRoles = new T_Sys_UserRole();
            EntityHelper.ConvertEntity2OtherEntity(userRoleDTO, userRoles);
            DTO.OperationResult result = new DTO.OperationResult();
            try
            {
                dbContext.SysUserRoles.Add(userRoles);
                dbContext.SaveChanges();

                #region 添加操作日志
                DTO.UserPermission userLog = new DTO.UserPermission();
                string content = "给角色分配角色！";
                string objectid = userRoles.ID.ToString();
                userLog.UserId = userRoles.IsrtUseID.ToString();
                userLog.UserName = "";
                userLog.FunctionId = DTO.FunctionId.人员关联角色;
                userLog.FunctionType = DTO.FunctionType.添加;
                InsertUserLog(content, objectid, userLog);
                #endregion

                result.Result = true;
                result.ReturnValue = null;
                result.Message = "用户角色添加成功！";
            }
            catch (Exception ex)
            {
                result.Message = "用户角色添加失败！" + ex.Message;
                result.Result = false;
            }
            return result;
        }

        /// <summary>
        /// 修改用户角色信息
        /// </summary>
        /// <param name="id">用户角色信息ID</param>
        /// <param name="roleGuid">角色GUID</param>
        /// <param name="userID">添加人GUID</param>
        /// <returns></returns>
        public override DTO.OperationResult UpdateUserRole(DTO.T_Sys_UserRole userRoleDTO)
        {
            T_Sys_UserRole userRoles = new T_Sys_UserRole();
            EntityHelper.ConvertEntity2OtherEntity(userRoleDTO, userRoles);
            DTO.OperationResult result = new DTO.OperationResult();
            try
            {
                var role = dbContext.SysUserRoles.Find(userRoles.ID);
                if (role != null)
                {
                    role.RoleGuid = userRoles.RoleGuid;
                    role.UserGuid = userRoles.UserGuid;
                    role.IsrtUseID = userRoles.IsrtUseID;
                    dbContext.SaveChanges();

                    #region 添加操作日志
                    DTO.UserPermission userLog = new DTO.UserPermission();
                    string content = "修改用户角色！";
                    string objectid = userRoles.ID.ToString();
                    userLog.UserId = userRoles.IsrtUseID.ToString();
                    userLog.UserName = "";
                    userLog.FunctionId = DTO.FunctionId.人员关联角色;
                    userLog.FunctionType = DTO.FunctionType.修改;
                    InsertUserLog(content, objectid, userLog);
                    #endregion

                    result.Result = true;
                    result.ReturnValue = null;
                    result.Message = "用户角色修改成功！";
                }
            }
            catch (Exception ex)
            {
                result.Message = "用户角色修改失败！" + ex.Message;
                result.Result = false;
            }
            return result;
        }

        /// <summary>
        /// 删除用户角色
        /// </summary>
        /// <param name="roleID">角色ID或角色GUID</param>
        /// <param name="type">0:通过ID,1:通过Guid</param>
        /// <returns></returns>
        public override DTO.OperationResult DeleteUserRole(string roleGuid, string userGuid)
        {
            DTO.OperationResult result = new DTO.OperationResult();
            try
            {
                Guid roleguid = Guid.Parse(roleGuid);
                Guid userguid = Guid.Parse(userGuid);
                DbSet<T_Sys_UserRole> dbUserRoles = dbContext.SysUserRoles;
                var role = dbUserRoles.Where(o => o.RoleGuid == roleguid && o.UserGuid == userguid).FirstOrDefault();
                role.IsDel = true;
                dbContext.SaveChanges();

                #region 添加操作日志
                DTO.UserPermission userLog = new DTO.UserPermission();
                string content = "根据用户GUID和角色GUID判断是否存在此记录！如存在进行删除！(逻辑删除：设置IsDel字段为：true)";
                string objectid = role.ID.ToString();
                userLog.UserId = role.IsrtUseID.ToString();
                userLog.UserName = "";
                userLog.FunctionId = DTO.FunctionId.人员关联角色;
                userLog.FunctionType = DTO.FunctionType.删除;
                InsertUserLog(content, objectid, userLog);
                #endregion

                result.Result = true;
                result.ReturnValue = null;
                result.Message = "删除成功！";
            }
            catch (Exception ex)
            {
                result.Message = "删除失败！" + ex.Message;
                result.Result = false;
            }
            return result;
        }
        #endregion

        #region 控件管理
        /// <summary>
        /// 获取控件列表
        /// </summary>
        /// <param name="title"></param>
        /// <param name="method"></param>
        /// <param name="clientlMethod"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public override List<DTO.T_Sys_Control> GetControls(string title, string method, string clientlMethod, string description)
        {
            var query = from a in dbContext.SysControl
                        where a.IsDel == false
                        select a;

            if (!string.IsNullOrEmpty(title))
            {
                query = query.Where(c => c.CtrlTitle.Contains(title));

            }
            if (!string.IsNullOrEmpty(method))
            {
                query = query.Where(c => c.CtrlMethod.Contains(method));
            }
            if (!string.IsNullOrEmpty(clientlMethod))
            {
                query = query.Where(c => c.CtrlClientlMethod.Contains(clientlMethod));
            }
            if (!string.IsNullOrEmpty(description))
            {
                query = query.Where(c => c.CtrlDescription.Contains(description));
            }
            List<DTO.T_Sys_Control> lists = new List<DTO.T_Sys_Control>();
            try
            {
                List<T_Sys_Control> list = query.OrderByDescending(c => c.IsrtDate).ToList();

                foreach (T_Sys_Control item in list)
                {
                    DTO.T_Sys_Control control = new DTO.T_Sys_Control();
                    EntityHelper.ConvertEntity2OtherEntity(item, control);
                    lists.Add(control);
                }
            }
            catch (Exception e)
            {

            }
            return lists;
        }

        /// <summary>
        /// 根据控件guid获取控件信息
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public override DTO.T_Sys_Control GetControlByGuId(Guid guid)
        {
            T_Sys_Control control = dbContext.SysControl.Where(m => m.CtrlGuid == guid).FirstOrDefault();
            DTO.T_Sys_Control controls = new DTO.T_Sys_Control();
            EntityHelper.ConvertEntity2OtherEntity(control, controls);
            return controls;
        }
        /// <summary>
        /// 根据功能Guid获取该功能对应Control
        /// </summary>
        /// <param name="funguid"></param>
        /// <returns></returns>
        public override List<DTO.T_Sys_Control> GetControlByFunGuid(Guid funguid)
        {
            List<DTO.T_Sys_Control> controls = new List<DTO.T_Sys_Control>();
            try
            {
                List<T_Sys_Control> control = (from a in dbContext.SysConFunction
                                               from b in dbContext.SysControl
                                               where a.CtrlGuid == b.CtrlGuid && a.FunctionGuid == funguid
                                               && a.IsDel == false
                                               select b).ToList();
                foreach (T_Sys_Control item in control)
                {
                    DTO.T_Sys_Control Dcontrol = new DTO.T_Sys_Control();
                    EntityHelper.ConvertEntity2OtherEntity(item, Dcontrol);
                    controls.Add(Dcontrol);
                }
            }
            catch
            {

            }
            return controls;
        }
        /// <summary>
        /// 添加控件
        /// </summary>
        /// <param name="controlDTO"></param>
        /// <returns></returns>
        public override DTO.OperationResult AddControl(DTO.T_Sys_Control controlDTO)
        {
            T_Sys_Control control = new T_Sys_Control();
            EntityHelper.ConvertEntity2OtherEntity(controlDTO, control);
            DTO.OperationResult result = new DTO.OperationResult();
            try
            {
                dbContext.SysControl.Add(control);
                dbContext.SaveChanges();

                #region 添加操作日志
                DTO.UserPermission userLog = new DTO.UserPermission();
                string content = "添加控件信息！";
                string objectid = control.ID.ToString();
                userLog.UserId = new Guid().ToString();
                userLog.UserName = "";
                userLog.FunctionId = DTO.FunctionId.控件模块;
                userLog.FunctionType = DTO.FunctionType.添加;
                InsertUserLog(content, objectid, userLog);
                #endregion

                result.Result = true;
                result.ReturnValue = null;
                result.Message = "添加成功！";
            }
            catch (Exception ex)
            {
                result.Message = "添加失败！" + ex.Message;
                result.Result = false;
            }
            return result;
        }

        /// <summary>
        /// 修改控件信息
        /// </summary>
        /// <param name="controlDTO"></param>
        /// <returns></returns>
        public override DTO.OperationResult UpdateControl(DTO.T_Sys_Control controlDTO)
        {
            DTO.OperationResult result = new DTO.OperationResult();
            try
            {
                var control = dbContext.SysControl.Where(c => c.CtrlGuid == controlDTO.CtrlGuid).FirstOrDefault();
                if (control != null)
                {
                    control.CtrlID = controlDTO.CtrlID;
                    control.CtrlTitle = controlDTO.CtrlTitle;
                    control.CtrlMethod = controlDTO.CtrlMethod;
                    control.CtrlClientlMethod = controlDTO.CtrlClientlMethod;
                    control.FuncType = controlDTO.FuncType;
                    control.CtrlDescription = controlDTO.CtrlDescription;
                    dbContext.SaveChanges();

                    #region 添加操作日志
                    DTO.UserPermission userLog = new DTO.UserPermission();
                    string content = "修改控件信息！";
                    string objectid = control.ID.ToString();
                    userLog.UserId = new Guid().ToString();
                    userLog.UserName = "";
                    userLog.FunctionId = DTO.FunctionId.控件模块;
                    userLog.FunctionType = DTO.FunctionType.修改;
                    InsertUserLog(content, objectid, userLog);
                    #endregion

                    result.Result = true;
                    result.ReturnValue = null;
                    result.Message = "修改成功！";
                }
            }
            catch (Exception ex)
            {
                result.Message = "修改失败！" + ex.Message;
                result.Result = false;
            }
            return result;
        }

        /// <summary>
        /// 删除控件
        /// </summary>
        /// <param name="controlGuid"></param>
        /// <returns></returns>
        public override DTO.OperationResult DeleteControl(string controlGuid)
        {
            DTO.OperationResult result = new DTO.OperationResult();
            try
            {
                Guid controlguid = Guid.Parse(controlGuid);
                var control = dbContext.SysControl.Where(o => o.CtrlGuid == controlguid).FirstOrDefault();
                control.IsDel = true;
                dbContext.SaveChanges();

                #region 添加操作日志
                DTO.UserPermission userLog = new DTO.UserPermission();
                string content = "删除控件信息！(删除：设置IsDel字段为：false)";
                string objectid = control.ID.ToString();
                userLog.UserId = new Guid().ToString();
                userLog.UserName = "";
                userLog.FunctionId = DTO.FunctionId.控件模块;
                userLog.FunctionType = DTO.FunctionType.删除;
                InsertUserLog(content, objectid, userLog);
                #endregion

                result.Result = true;
                result.ReturnValue = null;
                result.Message = "删除成功！";
            }
            catch (Exception ex)
            {
                result.Message = "删除失败！" + ex.Message;
                result.Result = false;
            }
            return result;
        }
        #endregion

        /// <summary>
        /// 获取用户数量
        /// </summary>
        /// <param name="corpName"></param>
        /// <returns></returns>
        public override int GetUserCount(string corpName)
        {
            var query = from a in dbContext.T_Sys_Users
                        select a;

            if (!string.IsNullOrEmpty(corpName))
            {
                query = query.Where(r => r.DeptName.Contains(corpName));

            }
            int count = query.ToList().Count();
            return count;
        }
    }
}
